package cn.dzhy.myzf.qq.email.api;

import cn.dzhy.myzf.qq.email.model.Party;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.foxinmy.weixin4j.exception.WeixinException;
import com.foxinmy.weixin4j.http.weixin.ApiResult;
import com.foxinmy.weixin4j.http.weixin.WeixinResponse;
import com.foxinmy.weixin4j.model.Token;
import com.foxinmy.weixin4j.token.TokenManager;
import java.math.BigInteger;
import java.util.List;

/**
 * 腾讯企业邮箱的部门接口实现
 * https://exmail.qq.com/qy_mng_logic/doc#10012
 * @className PartyApi
 * @author myzf(yangzhuangfei_min@odao.com)
 * @date 2018/3/15 下午10:38
 * @since JDK 1.8
 * @see cn.dzhy.myzf.qq.email.api
 */
public class PartyApi extends EmailApi {
	private final TokenManager tokenManager;

	public PartyApi(TokenManager tokenManager) {
		this.tokenManager = tokenManager;
	}

	/**
	 * 创建部门
	 * @see__https://exmail.qq.com/qy_mng_logic/doc#10008
	 * @className PartyApi
	 * @author myzf(yangzhuangfei_min@odao.com)
	 * @date 2018/3/15 下午10:41
	 * @since JDK 1.8
	 * @see cn.dzhy.myzf.qq.email.api
	 * @return 系统随机生成的部门id
	 * @throws WeixinException
	 */
	public int createParty(Party party) throws WeixinException {
		String department_create_uri = getRequestUri("department_create_uri");
		JSONObject obj = (JSONObject) JSON.toJSON(party);
		if (party.getParentId().compareTo(new BigInteger("1")) == -1 ) {
			obj.remove("parentid");
		}
		if (party.getId().compareTo(new BigInteger("1")) == -1) {
			obj.remove("id");
		}
		Token token = tokenManager.getCache();
		WeixinResponse response = weixinExecutor.post(
				String.format(department_create_uri, token.getAccessToken()),
				obj.toJSONString());
		return response.getAsJson().getIntValue("id");
	}

	/**
	 * 更新部门
	 * @see__https://exmail.qq.com/qy_mng_logic/doc#10009
	 * @className PartyApi
	 * @author myzf(yangzhuangfei_min@odao.com)
	 * @date 2018/3/15 下午10:43
	 * @since JDK 1.8
	 * @see cn.dzhy.myzf.qq.email.api
	 * @return 微信状态码
	 * @throws WeixinException
	 */
	public ApiResult updateParty(Party party) throws WeixinException {
		if (party.getId().compareTo(new BigInteger("1")) == -1) {
			throw new WeixinException("department id must gt 1");
		}
		String department_update_uri = getRequestUri("department_update_uri");
		JSONObject obj = (JSONObject) JSON.toJSON(party);
		if (party.getParentId().compareTo(new BigInteger("1")) == -1 ) {
			obj.remove("parentid");
		}
		if (party.getOrder() < 0) {
			obj.remove("order");
		}
		Token token = tokenManager.getCache();
		WeixinResponse response = weixinExecutor.post(
				String.format(department_update_uri, token.getAccessToken()),
				obj.toJSONString());
		return response.getAsResult();
	}

	/**
	 * 删除部门 （注：不能删除根部门；不能删除含有子部门、成员的部门）
	 * @see__https://exmail.qq.com/qy_mng_logic/doc#10010
	 * @className PartyApi
	 * @author myzf(yangzhuangfei_min@odao.com)
	 * @date 2018/3/15 下午10:44
	 * @since JDK 1.8
	 * @see cn.dzhy.myzf.qq.email.api
	 * @return 微信返回码
	 * @throws WeixinException
	 */
	public ApiResult deleteParty(BigInteger partId) throws WeixinException {
		String department_delete_uri = getRequestUri("department_delete_uri");
		Token token = tokenManager.getCache();
		WeixinResponse response = weixinExecutor.get(String.format(department_delete_uri, token.getAccessToken(), partId));
		return response.getAsResult();
	}
	/**
	 * 获取部门列表 获取指定部门及其下的子部门。id为1时可获取根部门下的子部门。
	 * @see__https://exmail.qq.com/qy_mng_logic/doc#10011
	 * @className PartyApi
	 * @author myzf(yangzhuangfei_min@odao.com)
	 * @date 2018/3/15 下午10:46
	 * @since JDK 1.8
	 * @see cn.dzhy.myzf.qq.email.api
	 * @return department的集合
	 * @throws WeixinException
	 */
	public List<Party> listParty(BigInteger partId) throws WeixinException {
		String department_list_uri = getRequestUri("department_list_uri");
		if (partId.compareTo(new BigInteger("0")) == 1 ) { department_list_uri += String.format("&id=%d", partId); }
		Token token = tokenManager.getCache();
		WeixinResponse response = weixinExecutor.get(String.format(
				department_list_uri, token.getAccessToken()));
		return JSON.parseArray(response.getAsJson().getString("department"),
				Party.class);
	}



	/**
	 * 查找部门 1/0：是否模糊匹配 true模糊查询 false匹配查询
	 * @see__https://exmail.qq.com/qy_mng_logic/doc#10012
	 * @className PartyApi
	 * @author myzf(yangzhuangfei_min@odao.com)
	 * @date 2018/3/15 下午10:47
	 * @since JDK 1.8
	 * @see cn.dzhy.myzf.qq.email.api
	 * @return 
	 * @throws WeixinException
	 */
	public List<Party> Partysearch(Party party) throws WeixinException {
		String department_search_uri = getRequestUri("department_search_uri");
		Token token = tokenManager.getCache();
		if (party != null){
			WeixinResponse response = weixinExecutor.post(String.format(department_search_uri, token.getAccessToken()), JSON.toJSONString(party));
			return JSON.parseArray(response.getAsJson().getString("department"),
					Party.class);
		}
		return null;
	}

}
